//
//  ShipingPlayViewController.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/4.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "ShipingPlayViewController.h"
#import "FitHeader.h"
#import "XinagqingCollectionViewCell.h"
#import "KrVideoPlayerController.h"
#import "XMHNetWorkongMethods.h"
#import "SingleTon.h"
#define POSTURL @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446688240625&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Info.getVideoUrl&virtual=&signmethod=md5&v=2&timestamp=1446688349&nonce=0.5325869858665556&appsign=f4355cc56ef4008ca0dafe48a510ffcf"
#define ImageURL @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446688240625&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Info.getInfo&virtual=&signmethod=md5&v=2&timestamp=1446721684&nonce=0.9213585244034408&appsign=ecf332814e3efedc687a93b9cf30676f"
#import "BuzhouCollectionViewCell.h"
#import "ShiwuViewController.h"
#import <UIImageView+WebCache.h>
#import "SingleTon.h"
@interface ShipingPlayViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,PushToFood>
@property (nonatomic, strong) KrVideoPlayerController  *videoController;
@property(nonatomic, strong)NSString *url;
@property(nonatomic, strong)UIImageView *myImageView;
@property(nonatomic, strong)UIButton *button;
@end

@implementation ShipingPlayViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.translucent = YES;
    self.navigationController.navigationBar.translucent = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
//    self.tabBarController.tabBar.translucent = NO;
   self.navigationController.navigationBar.translucent = YES;
    [_videoController pause];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    _huadongView = [[HuadongView alloc]initWithFrame:CGRectMake(0, 250 * FitHeight, self.view.frame.size.width, 55 * FitHeight)];
    [_huadongView.leftButton addTarget:self action:@selector(leftButton) forControlEvents:UIControlEventTouchUpInside];
    [_huadongView.rightButton addTarget:self action:@selector(rightButton) forControlEvents:UIControlEventTouchUpInside];
            CGFloat width = [UIScreen mainScreen].bounds.size.width;
    _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, width, 180 * FitHeight)];
    [self.view addSubview:_myImageView];
    _button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(150 * FitWidth, 120 * FitHeight, 64 * FitWidth, 64 * FitHeight);
    [_button setImage:[UIImage imageNamed:@"iconfont-bofang.png"] forState:0];
    _button.tintColor = [UIColor colorWithRed:0.12 green:0.63 blue:0.59 alpha:1];
    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
  
    
    [self.view addSubview:_huadongView];
    [self createCollectionView];
    [self.view addSubview:_collectionView];
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中,不要着急";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.75 green:0.77 blue:0.77 alpha:1];
    [self.view addSubview:_progressView];
    
}
- (void)playVideo
{
    NSURL *url = [NSURL URLWithString:_url];
    [self addVideoPlayerWithURL:url];
}

-(void)buttonAction
{
    [self playVideo];
    [_myImageView removeFromSuperview];
}

-(void)pushToFood:(NSString *)foodId Name:(NSString *)name
{
    self.hidesBottomBarWhenPushed = YES;
    ShiwuViewController *vc = [[ShiwuViewController alloc]init];
    vc.foodId = foodId;
    vc.name = name;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getData
{
    [XMHNetWorkongMethods getDataVyString:POSTURL BodyString:[NSDictionary dictionaryWithObjectsAndKeys:[SingleTon shareSingleTon].rid,@"rid", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        _url = [dic objectForKey:@"Url"];
        [_collectionView reloadData];
       // [self playVideo];
    }];
    [XMHNetWorkongMethods getDataVyString:ImageURL BodyString:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"uid",[SingleTon shareSingleTon].rid,@"rid",@"a6afb132369a979ba3427eec69189063",@"uuid", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [[data objectForKey:@"result"]objectForKey:@"info"];
        [_myImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"Cover"]]];
        [_progressView removeFromSuperview];

    } WithFailure:^(NSError *error) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"网络故障" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [alertController removeFromParentViewController];
            [_progressView removeFromSuperview];
        }];
        UIAlertAction *going = [UIAlertAction actionWithTitle:@"继续加载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self getData];
            [alertController removeFromParentViewController];
        }];
        [alertController addAction:ok];
        [alertController addAction:going];
        [self presentViewController:alertController animated:YES completion:nil];
    }];
    }
- (void)addVideoPlayerWithURL:(NSURL *)url{
    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 64, width, 180 * FitHeight)];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        [self.videoController setWillBackOrientationPortrait:^{
            [weakSelf toolbarHidden:NO];
        }];
        [self.videoController setWillChangeToFullscreenMode:^{
            [weakSelf toolbarHidden:YES];
        }];
        [self.view addSubview:self.videoController.view];
    }
    self.videoController.contentURL = url;
    
}
//隐藏navigation tabbar 电池栏
- (void)toolbarHidden:(BOOL)Bool
{
    self.navigationController.navigationBar.hidden = Bool;
    self.tabBarController.tabBar.hidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
}

-(void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 255 * FitHeight);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 320 * FitHeight, self.view.frame.size.width, self.view.frame.size.height - 255 * FitHeight) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[XinagqingCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collectionView registerClass:[BuzhouCollectionViewCell class] forCellWithReuseIdentifier:@"identifier1"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        XinagqingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath ];
        cell.delegate = self;
        return cell;
    }
    else
    {
        BuzhouCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier1" forIndexPath:indexPath];
        return cell1;
    }
   
}

-(void)leftButton
{
    [_collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
}
-(void)rightButton
{
    [_collectionView setContentOffset:CGPointMake(375 * FitWidth, 0) animated:NO];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_collectionView == scrollView) {
        if (scrollView.contentOffset.x > 200 * FitWidth) {
            [_huadongView rightButtonAction];
        }
        else
        {
            [_huadongView leftButtonAction];
        }
    }
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
